package com.mytrip.service.impl;

import java.util.Date;
import java.util.List;

import com.mytrip.base.service.impl.BaseManagerImpl;
import com.mytrip.dao.AddressBooksDAO;
import com.mytrip.dao.AuthorityDAO;
import com.mytrip.dao.MemberAuthorityDAO;
import com.mytrip.dao.MemberDAO;
import com.mytrip.dao.UserinfoDAO;
import com.mytrip.model.AddressBooks;
import com.mytrip.model.AddressBooksExample;
import com.mytrip.model.Userinfo;
import com.mytrip.model.UserinfoExample;
import com.mytrip.service.UserManager;
import com.mytrip.webapp.filter.CtxSessionBag;

public class UserManagerImpl extends BaseManagerImpl implements UserManager{

	private AuthorityDAO authorityDao;

	private MemberDAO memberDAO;

	private MemberAuthorityDAO memberAuthorityDAO;

	private UserinfoDAO userinfoDAO;

	private AddressBooksDAO addressBooksDAO;

	public void setAddressBooksDAO(AddressBooksDAO addressBooksDAO){
		this.addressBooksDAO = addressBooksDAO;
	}

	public void setAuthorityDao(AuthorityDAO authorityDao){
		this.authorityDao = authorityDao;
	}

	public void setMemberDAO(MemberDAO memberDAO){
		this.memberDAO = memberDAO;
	}

	public void setMemberAuthorityDAO(MemberAuthorityDAO memberAuthorityDAO){
		this.memberAuthorityDAO = memberAuthorityDAO;
	}

	public void setUserinfoDAO(UserinfoDAO userinfoDAO){
		this.userinfoDAO = userinfoDAO;
	}

	@Override
	public Userinfo findUserByEmailOrMobile(String userName, String password){
		UserinfoExample example = new UserinfoExample();
		example.createCriteria().andEmailEqualTo(userName).andPwdEqualTo(password);

		List<Userinfo> list = userinfoDAO.selectByExample(example);
		if (list.size() != 0) {
			return list.get(0);
		} else {
			example = new UserinfoExample();
			example.createCriteria().andMobileEqualTo(userName).andPwdEqualTo(password);
			list = userinfoDAO.selectByExample(example);
			if (list.size() != 0) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public Integer updatePasswordAndEmail(Userinfo userinfo){
		userinfo.setIsPublic("0");
		userinfo.setUserStatus("0");
		userinfo.setMid(11);
		Date regTime = new Date();
		userinfo.setRegTime(regTime);
		return userinfoDAO.insert(userinfo);
	}
	
	@Override
	public Integer updateUserinfo(Userinfo userinfo) {
		return userinfoDAO.insert(userinfo);
	}

	@Override
	public void deleteAddressBooks(AddressBooks addressBooks){
		addressBooksDAO.deleteByPrimaryKey(addressBooks.getId());
	}

	@Override
	public List<AddressBooks> findAddressBooksByUser(){
		Integer uid = CtxSessionBag.getSessionBag().getUserinfo().getId();

		AddressBooksExample example = new AddressBooksExample();
		example.createCriteria().andUseridEqualTo(uid);
		return addressBooksDAO.selectByExample(example);
	}

	@Override
	public Integer saveAddressBooks(AddressBooks addressBooks){
		Integer uid = CtxSessionBag.getSessionBag().getUserinfo().getId();
		
		AddressBooksExample example = new AddressBooksExample();
		example.createCriteria().andUseridEqualTo(uid);
		AddressBooks record = new AddressBooks();
		record.set_default("0");
		addressBooksDAO.updateByExampleSelective(record, example);
		
		addressBooks.set_default("1");
		addressBooks.setUserid(uid);
		return addressBooksDAO.insert(addressBooks);
	}

	@Override
	public AddressBooks findAddressBooksById(Integer id){
		return addressBooksDAO.selectByPrimaryKey(id);
	}

}
